<template>
  <div id="sales">
    <h1>我的订单</h1>
    <div v-if="orders.length">
      <div v-for="order in orders" :key="order.id" class="order">
        <h2>订单号: {{ order.id }}</h2>
        <p>日期: {{ order.date }}</p>
        <p>总金额: {{ order.totalPrice | moneyFormat }}</p>
        <p>商品数量: {{ order.totalAmount }}</p>
        <div class="items">
          <div v-for="item in order.items" :key="item.goods_id" class="item">
            <img :src="item.thumb_url" />
            <p>{{ item.goods_name }}</p>
            <p>{{ item.price | moneyFormat }}</p>
            <p>数量: {{ item.buy_count }}</p>
          </div>
        </div>
      </div>
    </div>
    <div v-else>
      <p>您还没有订单。</p>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  computed: {
    ...mapState(['orders']),
  },
  mounted() {
    let user_id = this.$store.state.userInfo.id;
    this.reqOrders({ user_id });
  },
  methods: {
    ...mapActions(['reqOrders']),
    moneyFormat(value) {
      return `¥${(value / 100).toFixed(2)}`;
    }
  }
}
</script>

<style scoped>
#sales {
  padding: 20px;
}

.order {
  border: 1px solid #ccc;
  padding: 10px;
  margin-bottom: 10px;
}

.items {
  display: flex;
  flex-wrap: wrap;
}

.item {
  width: 200px;
  margin-right: 10px;
}
</style>
